package com.menglei.dailytasks.service;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.menglei.dailytasks.R;

public class ServiceBind extends Service {
    private static final String TAG = "ServiceBind";
    private MyBinder myBinder = new MyBinder();
    private MediaPlayer player;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG,"ServiceBind onBind");
        return myBinder;
    }

    public class MyBinder extends Binder{
        public void onPlay(){
            Log.d(TAG,"MyBinder  onPlay");
            player.start();
        }
        public void onPause(){
            Log.d(TAG,"MyBinder  onPause");
            if (player.isPlaying()){
                player.pause();
            }
        }
    }


    @Override
    public void onDestroy() {
        Log.d(TAG,"ServiceBind onDestroy");
        super.onDestroy();
    }

    @Override
    public void onCreate() {
        Log.d(TAG,"ServiceBind onCreate");
        player = MediaPlayer.create(this, R.raw.video);
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG,"ServiceBind onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onRebind(Intent intent) {
        Log.d(TAG,"ServiceBind onRebind");
        super.onRebind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG,"ServiceBind onUnbind");
        return super.onUnbind(intent);
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        Log.d(TAG,"ServiceBind onConfigurationChanged");
        super.onConfigurationChanged(newConfig);
    }
}
